<template>
  <div class="tableComponent">
    <el-table
		
      class="table-box"
      :header-cell-style="hearderStyle"
      :cell-style="cellStyle"
      :data="tableInfo.tableData"
      border
      :highlight-current-row="true"
      style="width: 100%; height: calc(100% - 60px)"
      ref="publicTable"
      v-loading="isLoading"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(0, 0, 0, 0.5)"
      @select="handleSelectionChange"
      @select-all="selectAll"
      @row-click="rowClick"
      :pictLoading="false"
    >
      <template>
        <el-table-column v-if="tableInfo.selection" align="center" type="selection" :selectable="selectable"></el-table-column>
        <el-table-column v-if="tableInfo.index" label="序号" width="80" align="center" type="index"></el-table-column>
      </template>
      <template v-for="(item, index) in tableInfo.tableConfig">
        <el-table-column
          :key="index"
          :label="item.label"
          :sortable="item.sortable || false"
          :fixed="item.fixed || false"
          :align="item.align || 'center'"
          :min-width="item.minWidth || ''"
          :width="item.width || ''"
          :formatter="item.renderFun"
          :show-overflow-tooltip="!item.tooltip"
          :prop="item.prop"
          :filters="item.filters ? item.filters : null"
          :filter-method="item.filterMethod"
          :filter-multiple="item.multiple"
        >
          <template slot-scope="scope">
            <div v-if="item.type === 'images'">
              <el-image
                :key="index"
                style="width: 80px; height: 80px"
                :src="Array.isArray(scope.row[item.prop]) ? scope.row[item.prop][0] : scope.row[item.prop]"
                :preview-src-list="Array.isArray(scope.row[item.prop]) ? scope.row[item.prop] : [scope.row[item.prop]]"
              >
                <div slot="error" class="image-slot" style="width: 80px; height: 80px; line-height: 80px; text-align: center">
                  <i class="el-icon-picture-outline" style="font-size: 40px"></i>
                </div>
              </el-image>
            </div>
            <el-switch
              v-else-if="item.type === 'switch'"
              :disabled="item.disableds"
              :active-text="item.active"
              :inactive-text="item.inactive"
              :active-value="item.activeValue"
              :inactive-value="item.inactiveValue"
             
              :active-color="'#EE5C5C'"
              :inactive-color="item.inactiveColor"
              v-model="scope.row[item.prop]"
              @change="item.events(scope.row)"
            ></el-switch>
            <el-rate v-else-if="item.type === 'rate'" v-model="scope.row[item.prop]" disabled></el-rate>
            <div v-else-if="item.type === 'buttons'" class="wrap">
              <span v-for="(litem, i) in item.list" :key="i">
                <el-button
                  style="margin-right: 10px"
                  v-if="litem.show(scope.row)"
                  @click.native.prevent="litem.events(scope.row, scope.$index)"
                  :type="litem.type"
                  :class="litem.type ? 'pulic-table-' + litem.type : 'pulic-table-'"
                  class="pulic-table"
                  size="mini"
                >
                  {{ litem.text }}
                </el-button>
                <el-dropdown v-if="litem.moreBtn && litem.moreBtnrow(scope.row)" trigger="click">
                  <el-button :type="litem.type" size="mini">{{ litem.text }}</el-button>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item v-for="(items, is) in litem.list" :key="is" @click.native.prevent="items.events(scope.row, scope.$index)">
                      <span v-if="items.show(scope.row)">{{ items.text }}</span>
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </span>
            </div>
            <span v-else-if="item.html" :class="item.class" @click="item.events ? item.events(scope.row) : null" v-html="item.html(scope.row[item.prop])"></span>
            <span v-else-if="item.htmls" :class="item.class" @click="item.events ? item.events(scope.row) : null" v-html="item.htmls(scope.row)"></span>
            <span v-else :class="item.class" @click="item.events ? item.events(scope.row) : null" v-html="scope.row[item.prop]"></span>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <div class="block " v-if="pagination">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="pageData.pindex"
        :page-sizes="pageSizes"
        :page-size="pageData.psize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageData.total"
        background
        v-if="pageData.total != 0"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
export default {
  name: "PublicTable",
  props: {
    // d_tableData:[],
    // 表头样式
    hearderStyle: {
      type: Object,
      default: function () {
        return {
          background: "#F5F5F5",
          color: "#333333",
          textAlign: "center",
          fontWeight: "bold",
        }
      },
    },
    // 单元格样式
    cellStyle: {
      type: Object,
      default: function () {
        return {
          fontSize: "12px",
          padding: "10px 0",
        }
      },
    },
    /*
    表格数据: tableData:[{}]
    表头信息: tableConfig：[{
            type: "",列类型，images（图片），switch（滑块按钮）buttons(按钮操作)
            prop: "",列属性
            label: "",列label
            width: "150px",列最小宽度
            activeValue: 1, switch打开属性
            inactiveValue: 0, switch关闭属性
            disableds：true/false switch是否禁用，默认false
            events: this.switchsChange, 单元格事件或switch事件
            class：单元格样式类名
            align：单元格对齐方式默认居中
            list:[{
              show(row)显示的条件
              events按钮点击事件
              type：按钮类型
              text:按钮描述
              tooltip:默认为假，传true不提示
            }]
            html 返回拼接后的数据

    }]
    是否有选择框：selection：true/false 默认false
    是否有序列号：index：true/false 默认false
    */
    tableInfo: {
      type: Object,
      default: () => {
        return {
          tableData: [],
          tableConfig: [],
          selection: false,
          index: false,
          selectable: false,
        }
      },
    },
    /*
     分页数据
     pindex:当前页码
     psize：每页条数
     total：总共多少条
    */
    pageData: {
      type: Object,
      default: () => {
        return {
          // pindex: 1,
          // psize: 10,
          // total: 0,
        }
      },
    },
    // 是否有分页
    pagination: {
      type: Boolean,
      default: false,
    },
    // 分页器每页条数
    pageSizes: {
      type: Array,
      default: () => [10, 20, 50, 100],
    },
    // 加载条
    isLoading: {
      type: Boolean,
      default: false,
    },
    // 吾品商城 商品管理 批量上下架 控制只有通过审核的商品才能选中
    goodSthrough: {
      type: Boolean,
      default: false,
    },
    // 盲盒管理 添加盲盒活动 添加奖品 控制没有库存的检品不能选
    blindBox: {
      type: Boolean,
      default: false,
    },
    selectArr: {
      type: Array,
      default: () => []
    },
  },
  watch:{
    //选中回显
    selectArr:{
      handler(newVal){
        this.$nextTick(() => {
          this.tableInfo.tableData.forEach(i => {
            newVal.forEach(item => {
              if(item == i.id){
                this.$refs.publicTable.toggleRowSelection(i,true);
              }
            })
          });
          // let list = this.tableInfo.tableData.filter(item => newVal.includes(item.id))
          // console.log(list)
          // if(list.length > 0)
          // list.forEach(item => {
          //   this.$refs.publicTable.toggleRowSelection(item,true);
          // })
        })
      },
      // deep: true,
    },
  },
  methods: {
    // 每页多少条
    handleSizeChange(psize) {
      this.pageData.psize = psize
      this.$emit("handleSizeChange", psize)
    },
    // 当前页码
    handleCurrentChange(pindex) {
      this.pageData.pindex = pindex
      this.$emit("handleCurrentChange", pindex)
    },
    // 多选
    handleSelectionChange(val, row) {
      // 通过selectionData事件把选中的数据返回给父组件
      this.$emit("selectionDatas", val,row)
    },
    // 全选
    selectAll(val) {
      // 通过selectionData事件把选中的数据返回给父组件
      this.$emit("selectionData", val)
    },
    //某一行被点击
    rowClick(e){
      this.$emit("clickRow", e)
    },
    // 独立全选
    toggleSelection(selectBottom) {
      selectBottom ? this.$refs.publicTable.toggleAllSelection() : this.$refs.publicTable.clearSelection()
      // this.$emit("selectionData", val)
    },
    // 独立单选
    toggleSelectionSingle(row) {
      this.$nextTick(()=>{
        this.$refs.publicTable.toggleRowSelection(row)
      })
    },
    //选项框是否可选
    selectable(row, index) {
      if (this.tableInfo.selectable && this.tableInfo.selectable != false) {
        //吾品商城 商品管理 批量上下架 控制只有通过审核的商品才能选中
        if(this.goodSthrough){
          if(row.status != 2){
            return false
          }else return true
        } 
        //盲盒管理 添加盲盒活动 添加奖品 控制没有库存的检品不能选
        if(this.blindBox){
          if(!row.inventory){
            return false
          }else return true
        } 
        //结束
        if (row[this.tableInfo.selectable.type] == this.tableInfo.selectable.value) {
          return true
        }else {
          return false
        }
      } else {
        return true
      }
    },
  },
}
</script>
<style lang="scss" scoped>
.tableComponent {
  height: 100%;
  .wrap{
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
  }
  .block {
    text-align: right;
    margin-top: 10px;
  }
  .el-button--mini,
  .el-button--mini.is-round {
    padding: 7px;
  }
  .el-table {
    .el-table__fixed-right {
      height: 100% !important; //设置高优先，以覆盖内联样式  处理固定行高度不高显示不全问题
    }
    .statistics-warning-row {
      background-color: #dfdfdf ;
    }
  }
}
</style>
